class Solution:
    def findRedundantConnection(self, edges):
        n=len(edges)
        links=[[] for _ in range(n)]
        for e in edges:
            links[e[0]-1].append(e[1]-1)
            links[e[1]-1].append(e[0]-1)
        print(links)
        def searchnodes(now,exclude):
            nonlocal n
            nodes=[0]*n
            count=1
            stack=[now]
            nodes[now]=1
            while stack:
                i= stack.pop()
                for k in links[i]:
                    if i==now and k==exclude:
                        continue
                    if nodes[k]==0:
                        stack.append(k)
                        nodes[k]=1
                        count+=1
            if count!=n:
                return False
            else:
                return True
        edges.reverse()
        for e in edges:
            if searchnodes(e[0]-1,e[1]-1):
                return e

s=Solution()
print(s.findRedundantConnection([[1,2], [2,3], [3,4], [1,4], [1,5]]))

'''
执行用时：
68 ms
, 在所有 Python3 提交中击败了
61.41%
的用户
内存消耗：
15.3 MB
, 在所有 Python3 提交中击败了
17.89%
的用户
'''